package com.marketingexpert.controller.noticemgnt;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.config.Configuration;
import com.marketingexpert.domain.Notice;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.noticemgnt.NoticeService;

public class QueryNewNoticeController implements Controller {

	private static Logger logger = Logger.getLogger(QueryNewNoticeController.class);
	
	private static Calendar calendar = Calendar.getInstance();
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private NoticeService noticeService;
	
	private static String endTime; 
	private static String startTime; 
	
	static {
		endTime = sdf.format(calendar.getTime());
		calendar.add(Calendar.DATE, -7);
		startTime = sdf.format(calendar.getTime());
	}
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=GBK");  
		PrintWriter out = response.getWriter();
		
		try {
		Notice notice = noticeService.queryNewNotice(startTime,endTime);
		if(notice != null) {
			out.print(notice.getNoticeContent());
		} else {
			out.print("");
		}		
		} catch(MarketingExpertException e) {
			logger.error(e.getMessage(), e);
			out.print("");
		}
		return null;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}
}
